package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.domain.Sso;
import cn.itsource.hrm.domain.VipBase;
import cn.itsource.hrm.dto.RegisterDTO;
import cn.itsource.hrm.dto.SMSSendDTO;
import cn.itsource.hrm.feignclients.RedisFeignClient;
import cn.itsource.hrm.mapper.SsoMapper;
import cn.itsource.hrm.mapper.VipBaseMapper;
import cn.itsource.hrm.service.ISsoService;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.encrypt.MD5;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author luobiao
 * @since 2020-03-02
 */
@Service
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {
    @Autowired
    private VipBaseMapper vipBaseMapper;

    @Autowired
    private RedisFeignClient redisFeignClient;
    @Override
    public void register(RegisterDTO registerDTO) {
        //1.参数判断
        if(!StringUtils.hasLength(registerDTO.getMobile())||
                !StringUtils.hasLength(registerDTO.getPassword())||
                !StringUtils.hasLength(registerDTO.getSmsCode())){
            throw new RuntimeException("无效的参数");
        }
        //2.判断手机验证码是否正确
        //redis中保存手机验证码的key值
        String smsCodeRedisKey = "sms:"+registerDTO.getMobile();
        //获取redis中的手机验证码
        AjaxResult ajaxResult = redisFeignClient.get(smsCodeRedisKey);
        if(!ajaxResult.isSuccess()||ajaxResult.getResultObj()==null){
            throw new RuntimeException("无效的手机验证码");
        }
        String smsCode = JSON.parseObject(ajaxResult.getResultObj().toString(), SMSSendDTO.class).getCode();
        if(!registerDTO.getSmsCode().equals(smsCode)){
            throw new RuntimeException("手机验证码错误");
        }
        //判断是否有该用户，通过手机号查询是否有该用户
        Sso sso = baseMapper.selectByPhone(registerDTO.getMobile());
        if(sso != null){
            throw new RuntimeException("该手机号已经注册，请换一个手机号注册");
        }
        //保存sso
        //当前系统时间
        long time = new Date().getTime();
        Sso registerSso = new Sso();
        registerSso.setCreateTime(time);
        registerSso.setPhone(registerDTO.getMobile());
        //注意：密码加盐值，加密
        //随机产生一个盐值
        String salt = MD5.getRandomCode(16);
        String md5Password = MD5.getMD5(registerDTO.getPassword() + salt);
        //设置密码
        registerSso.setPassword(md5Password);
        //设置盐值
        registerSso.setSalt(salt);
        baseMapper.insert(registerSso);
        //保存base
        VipBase vipBase = new VipBase();
        vipBase.setSsoId(registerSso.getId());

        vipBase.setCreateTime(time);
        vipBase.setRegTime(time);
        vipBase.setRegChannel(1); //1.PC端注册

        vipBaseMapper.insert(vipBase);
    }

    /*
    * 登录
    * */
    @Override
    public String login(Sso sso) {
        //参数判断
        if(!StringUtils.hasLength(sso.getPassword()) || !StringUtils.hasLength(sso.getPhone())){
            throw new RuntimeException("无效的登录信息");
        }
        //根据手机号查询Mysql中的sso
        Sso mysqlSso = baseMapper.selectByPhone(sso.getPhone());
        if(mysqlSso==null){
            throw new RuntimeException("无此用户");
        }
        //获取盐值和参数中密码加密
        String salt = mysqlSso.getSalt();
        String md5Password = MD5.getMD5(sso.getPassword() + salt);
        //加密后的密码和数据库中加密的密码进行比较
        if(!mysqlSso.getPassword().equals(md5Password)){
            throw new RuntimeException("密码错误");
        }
        //登录成功，创建token(Redis的key)
        String token = UUID.randomUUID().toString();
        //把登录信息存储到Redis ： 如果Redis过期了怎么处理？
        AjaxResult ajaxResult = redisFeignClient.setex(token, 60 * 60, JSON.toJSONString(mysqlSso));
        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("登录失败");
        }
        //7.返回token给客户端
        //1.Token直接写到cookies
        //2.直接返回token
        return token;
    }
}
